import cv2
import camera_ctrl as ccl
#import 
import xlsxwriter



def save(data,bd,offset,path_and_name):
    workbook = xlsxwriter.Workbook(path_and_name)    
    worksheet = workbook.add_worksheet('RR')
    worksheet.write(0,0,"bd1\bd2")
    [h, l] = data.shape  # h为行数，l为列数
    for i in range(h):
        for j in range(l):
            worksheet.write(i+offset[0], j+offset[1], data[i][j])
            
    for i in range(len(bd)):
            worksheet.write(i+1, 0,bd[i])
            worksheet.write(0, i+1,bd[i])
    workbook.close()
    print('data save:',path_and_name)
    
#save(rr,bd1,[1,1],save_path+name_by_time()+str(max_bd)+'nm'+save_excel_name)


class cam_set_class:
    def __init__(self):
        print("use camera contral...")
    def info(self):
        print("hello")
    def show(self,str):
        print("input is ",str)
    a=0
    brightness=0
    contrast = 0
    saturation=0
    hue =0
    gain = 0
    expore = 0


def nothing(x):
    pass


#鼠标回调函数 
#事件 坐标 标志 返回

def show_num(event,y,x,flags,param):
    if event==cv2.EVENT_LBUTTONUP:
        #print('POS:(',x,y,') R:',r,'G:',g,'B:',b)
        #print('POS:(',x,y,')' ,img.item(x,y,0),img.item(x,y,1),img.item(x,y,2))
        print('POS:(',x,y,')' ,frame[x][y])
        cv2.circle(frame,(y,x),10,(0,255,0),1)
        cv2.putText(frame,str(frame[x][y]),(y,x), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255),1)
    elif event==cv2.EVENT_MBUTTONDOWN:
        print(num)

cv2.namedWindow('image',cv2.WINDOW_NORMAL)
cv2.namedWindow('frame')


cv2.createTrackbar('Brightness','image',0,64,nothing)
cv2.createTrackbar('contrast','image',0,95,nothing)
cv2.createTrackbar('saturation','image',0,100,nothing)
cv2.createTrackbar('hue','image',0,4096,nothing)
cv2.createTrackbar('gain','image',0,60,nothing)  # 0  ~ 60
cv2.createTrackbar('expore','image',0,13,nothing)#-10 ~  3


cv2.setMouseCallback('frame',show_num)

camset=cam_set_class()
camset.info()
camset.a=100
camset.show(camset.a)


#-------------------------------------
cap = cv2.VideoCapture(0)


cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1024)#//宽度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 768)#//高度
print(ccl.get_camera_info(cap))

cv2.setTrackbarPos('Brightness','image',51)
cv2.setTrackbarPos('contrast','image',53)
cv2.setTrackbarPos('saturation','image',81)
cv2.setTrackbarPos('hue','image',2048)
cv2.setTrackbarPos('gain','image',0)
cv2.setTrackbarPos('expore','image',10)



def send_parm(event,y,x,flags,param):
    if event==cv2.EVENT_LBUTTONUP:
        print('='*20)
        print('brightness',camset.brightness)
        print('contrast',camset.contrast)
        print('saturation',camset.saturation)
        print('hue',camset.hue)
        print('gain',camset.gain)
        print('expore',camset.expore)
        print('='*20)
        
        #cap.set(cv2.CAP_PROP_FPS, 30)#//帧率 帧/秒
        cap.set(cv2.CAP_PROP_BRIGHTNESS, camset.brightness)#//亮度 
        cap.set(cv2.CAP_PROP_CONTRAST,camset.contrast)#//对比度 40
        cap.set(cv2.CAP_PROP_SATURATION, camset.saturation)#//饱和度 50
        cap.set(cv2.CAP_PROP_HUE, camset.hue)#//色调 50
        cap.set(cv2.CAP_PROP_GAIN, camset.gain)#增益
        cap.set(cv2.CAP_PROP_EXPOSURE, camset.expore)#//曝光 50 
        print(ccl.get_camera_info(cap))

cv2.setMouseCallback('image',send_parm)
    

while(True):
    #read camera 
    ret,frame = cap.read()
    #gray = cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
    cv2.imshow('frame',frame)
    if(ret == True):
        key=cv2.waitKey(1)&0xFF
        if key==27:
            print("exit ..")
            break
        elif key==ord('r'):
            print(ccl.get_camera_info(cap))
        elif key == ord('s') :# s press
            cv2.imwrite('./img/cam.png',frame)
            print("save..")

        if camset.brightness==-1:
            print("exit 0 ..")
            break


   



cap.release()
cv2.destroyAllWindows()